#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <ctype.h>
#include "myjni.h"
#include "yuv_30w.h"
#include "yuv_200w.h"



void argb2yuv420sp(unsigned char* src_p, unsigned char* alph,                                                                                            
                int width, int height,
                unsigned char* dest_y, unsigned char* dest_c)
{
    int i,j;
    for(i = 0; i < (int)height; i++)
    {
        if((i&1) == 0)
        {
            for(j= 0; j< (int)width; j++)
            {
                *dest_y = (299*src_p[2]+587*src_p[1]+114*src_p[0])/1000;

                if((j&1) == 0)
                {
                   //cb
                   *dest_c++ = 128+(564*(src_p[0]-*dest_y)/1000);
                }
                else
                {
                   // cr
                   *dest_c++ = 128+(713*(src_p[2]-*dest_y)/1000);
                }

                *alph++ = src_p[3];
                src_p +=4;
                dest_y++;
            }
        }
        else
        {
            for(j= 0; j< (int)width; j++)
            {
                *dest_y = (299*src_p[2]+587*src_p[1]+114*src_p[0])/1000;
                *alph++ = src_p[3];
                src_p +=4;
                dest_y++;
            }
        }
    }

    return;
}


void yuv420sp_blending(int bg_width, int bg_height, int left, unsigned int top, int fg_width, int fg_height,
                       unsigned char *bg_y, unsigned char *bg_c, unsigned char *fg_y, unsigned char *fg_c, unsigned char *alph)
{
    unsigned char *bg_y_p = NULL;
    unsigned char *bg_c_p = NULL;
    int i = 0;
    int j = 0;

    bg_y_p = bg_y + top * bg_width + left;
    bg_c_p = bg_c + (top >>1)*bg_width + left;

    for(i = 0; i<(int)fg_height; i++)
    {
        if((i&1) == 0)
        {
            for(j=0; j< (int)fg_width; j++)
            {
                *bg_y_p = ((256 - *alph)*(*bg_y_p) + (*fg_y++)*(*alph))>>8;
                *bg_c_p = ((256 - *alph)*(*bg_c_p) + (*fg_c++)*(*alph))>>8;

                alph++;
                bg_y_p++;
                bg_c_p++;
            }

            bg_c_p = bg_c_p + bg_width - fg_width;
        }
        else
        {
            for(j=0; j< (int)fg_width; j++)
            {
                *bg_y_p = ((256 - *alph)*(*bg_y_p) + (*fg_y++)*(*alph))>>8;
                alph++;
                bg_y_p++;
            }
        }

        bg_y_p = bg_y_p + bg_width - fg_width;
    }
}

void yuv420sp_blending_adjust_brightness(int bg_width, int bg_height, int left, int top, int fg_width, int fg_height,
                       unsigned char *bg_y, unsigned char *bg_c, unsigned char *fg_y, unsigned char *fg_c, unsigned char *alph,
                       int is_brightness)
{
    unsigned char *bg_y_p = NULL;
    unsigned char *bg_c_p = NULL;

    int i = 0;
    int j = 0;

    if (bg_y == NULL || bg_c == NULL || fg_y == NULL || fg_c == NULL || alph == NULL) {
        ALOGE("<yuv420sp_blending_adjust_brightness>NULL pointer error!");
        return;
    }
    bg_y_p = bg_y + top * bg_width + left;
    bg_c_p = bg_c + (top >>1)*bg_width + left;

    if(is_brightness) {
        for(i = 0; i<fg_height; i++)
        {
            if((i&1) == 0)
            {
                for(j=0; j< fg_width; j++)
                {
                    *bg_y_p = ((256 - *alph)*(*bg_y_p) + (256 - (*fg_y++))*(*alph))>>8;
                    *bg_c_p = ((256 - *alph)*(*bg_c_p) + (*fg_c++)*(*alph))>>8;

                    alph++;
                    bg_y_p++;
                    bg_c_p++;
                }

                bg_c_p = bg_c_p + bg_width - fg_width;
            }
            else
            {
                for(j=0; j< fg_width; j++)
                {
                    *bg_y_p = ((256 - *alph)*(*bg_y_p) + (256 - (*fg_y++))*(*alph))>>8;
                    alph++;
                    bg_y_p++;
                }
            }

            bg_y_p = bg_y_p + bg_width - fg_width;
        }
    } else {
        for(i = 0; i<fg_height; i++)
        {
            if((i&1) == 0)
            {
                for(j=0; j< fg_width; j++)
                {
                    *bg_y_p = ((256 - *alph)*(*bg_y_p) + (*fg_y++)*(*alph))>>8;
                    //*bg_c_p = ((256 - *alph)*(*bg_c_p) + (*fg_c++)*(*alph))>>8;

                    alph++;
                    bg_y_p++;
                    //bg_c_p++;
                }

                //bg_c_p = bg_c_p + bg_width - fg_width;
            }
            else
            {
                for(j=0; j< fg_width; j++)
                {
                    *bg_y_p = ((256 - *alph)*(*bg_y_p) + (*fg_y++)*(*alph))>>8;
                    alph++;
                    bg_y_p++;
                }
            }

            bg_y_p = bg_y_p + bg_width - fg_width;
        }
    }
}


void addWaterMark(unsigned char*buf, int offset_h, int mFrameWidth, int mFrameHeight, char* str_date) {
    unsigned int i, j, k;
    //int offset_h = mFrameHeight/40;
    int offset_w = mFrameWidth/60;
    int y_start = mFrameWidth*offset_h + offset_w;
    unsigned char **p_y;
    unsigned int H, W;
    unsigned char * yuv_start;
    unsigned char **buf_yuv_p = (unsigned char **)malloc(mFrameWidth*mFrameHeight*sizeof(unsigned char **));
    int index = 0;
    H = 20; 
    W = 12;
    for (i = 0; i < (unsigned int)mFrameWidth*mFrameHeight; i ++)
        buf_yuv_p[i] = &buf[i*2];

    p_y = buf_yuv_p + y_start;
    yuv_start = (unsigned char*)yuv_30w;
    for (i = 0; i < strlen(str_date); i++) {
        char num = str_date[i];
        p_y += W;
        if (isdigit(num))
            index = atoi(&num);
        else if (num == ':')
            index = 10;
        else if (num == '-')
            index = 11;
        else if (num == ' ')
            continue;
        else if (num == '.')
            index=12;
        else if (num == 'J')
            index = 13;
        else if (num == 'W')
            index = 14;
        else
            LOGE("%s err num=%c", __func__, num);
        //LOGE("index=%d", index);
        for (j = 0; j < H; j++) {
            for (k = 0; k < W; k++) {
                //if (yuv_30w[index][j*W + k])
                //    p_y[j*mFrameWidth + k] = yuv_30w[index][j*W + k];
            unsigned char tmp = *(yuv_start + index*W*H*3/2 + j*W +k);
              if (tmp)
                  **(p_y + j*mFrameWidth + k) = tmp;
            }
        }
    }
    free((void *)buf_yuv_p);
}



